package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;


@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //    登录验证
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String password = employee.getPassword();
//        密码加密
        String s = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
//        条件查找
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(lqw);
        if (one == null) {
            return R.error("登录失败");
        }
        ;
        if (!one.getPassword().equals(s)) {
            return R.error("密码错误");
        }
        ;
        if (one.getStatus() == 0) {
            return R.error("该账号已被禁用");
        }
//        将Id存入到Session中
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);
    }

    //        员工退出登录界面
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
//        清理session中保存的员工id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    //    添加员工
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
//        设置初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
////        创建时间
//        employee.setCreateTime(LocalDateTime.now());
////        更新时间
//        employee.setUpdateTime(LocalDateTime.now());
//        创建人
//        Long employee1 = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(employee1);
////        修改人
//        employee.setUpdateUser(employee1);

        employeeService.save(employee);
        return R.success("添加成功");

    }

    //    分页查询
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
//        构造分页构造器
        Page page1 = new Page<>(page, pageSize);
//        条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
//        过滤条件
        lqw.like(StringUtils.isNotEmpty(name), Employee::getName, name);
//        查询
        employeeService.page(page1, lqw);

        return R.success(page1);
    }

    //    禁用启用
    @PutMapping
    public R<String> StartForbidden(HttpServletRequest request, @RequestBody Employee employee) {
//        修改时间
//        employee.setUpdateTime(LocalDateTime.now());
////        修改人
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    //    编辑回显数据
    @GetMapping("/{id}")
    public R<Employee> Echo(@PathVariable("id") Long id) {
        Employee byId = employeeService.getById(id);
        if (byId != null) {
            return R.success(byId);
        } else {
            return R.error("没有查询到对应员工信息");
        }
    }
}
